package com.hxd.otc.module.fiat_deal.presenter;

import android.support.annotation.NonNull;

import com.hxd.otc.api.IApi;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.base.BaseRecyclerPresenter;
import com.hxd.otc.module.fiat_deal.adapter.DealAdapter;
import com.hxd.otc.module.fiat_deal.bean.DealRequestBean;
import com.hxd.otc.module.fiat_deal.bean.DealResultDataBean;
import com.hxd.otc.module.fiat_deal.bean.DealTeamBean;
import com.hxd.otc.module.fiat_deal.contract.DealListContract;
import com.hxd.otc.module.fiat_deal.model.DealModel;
import com.hxd.otc.module.order.bean.AllOrderBean;
import com.hxd.otc.net.BaseObserver;

/**
 * Created by lzy on 2018/9/17 0017.
 * description ${交易列表Presenter}
 */
public class DealListPresenter extends BaseRecyclerPresenter<DealListContract.View, DealAdapter, DealResultDataBean.Data> implements DealListContract.Presenter {

    private DealModel mDealModel;
    private int mDealType;
    private String mCoinType;
    private String mFiat;

    private String loginSource = "otc";

    private DealTeamBean mDealTeamBean;
    private String mBankTypeValue;

    public DealListPresenter(DealListContract.View view) {
        super(view);
        mDealModel = new DealModel();
    }

    @Override
    public void loadData(final boolean isRefresh) {

        /**
         *http://192.168.1.196:8083/bbex/offline/advert/list?
         * coinId=e7ca5a25c7df4bcead954ade71aec7fe&supportCurrencyCode=CNY&
         * exType=0&loginSource=otc&currentPage=1&showCount=15 (93ms)
         * 2018-10-15 18:11:53.246 12828-14148/com.xhd.otc I/OTC0: RetrofitManager====transfer-encoding: chunked
         * @param coinId  法币币种id
         * @param supportCurrencyCode 虚拟币种
         * @param exType  // 买卖类型0买1卖
         * @param loginSource  写死otc
         * @param currentPage
         * @param showCount
         * @param observer
         * @param converter
         */
        mDealModel.getDealListData(mDealTeamBean.symbol, mFiat, mBankTypeValue, mDealType == 0 ? 1 : 0, loginSource, mPageIndex,
                getPageSize(), new BaseObserver<DealResultDataBean>() {
                    @Override
                    public void onNext(@NonNull String msg, @NonNull DealResultDataBean dealResultDataBean) {
                        onLoadDataSuccess(isRefresh, dealResultDataBean.list);
                    }

                    @Override
                    public void onError(int errType, @NonNull String errMessage) {
                        onLoadDataFail(isRefresh, errMessage);
                    }
                }, mView.<BaseBean<DealResultDataBean>>bindAutoDispose());
    }

    @Override
    public void setDealTeamBean(DealTeamBean dealTeamBean) {
        mDealTeamBean = dealTeamBean;
    }

    @Override
    public DealTeamBean getDealTeamBean() {
        return mDealTeamBean;
    }


    @Override
    public String getTransactionPair() {
        return String.format("%s/%s", mCoinType, mFiat);
    }

    @Override
    public void setDealType(int dealType) {
        mDealType = dealType;
    }

    @Override
    public int getDealType() {
        return mDealType;
    }

    @Override
    public String getCoinType() {
        return mCoinType;
    }

    @Override
    public void setFiat(String fiat) {
        mFiat = fiat;
    }

    @Override
    public String getFiat() {
        return mFiat;
    }

    @Override
    public void setBankTypeValue(String bankTypeValue) {
        mBankTypeValue = bankTypeValue;
    }

    @Override
    public String getBankTypeValue() {
        return mBankTypeValue;
    }

    @Override
    public void setCoinType(String coinType) {
        mCoinType = coinType;
    }

    @Override
    public void dealBuy(@NonNull final DealRequestBean bean) {

        mDealModel.deal(bean, new BaseObserver<AllOrderBean.DataBean>() {

            @Override
            public void onNext(@NonNull String msg, @NonNull AllOrderBean.DataBean dataBean) {
                mView.buySuccess(dataBean);
            }


            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.onDealFail();
                mView.onHideLoading();
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<AllOrderBean.DataBean>>bindAutoDispose());
    }

    @Override
    public void dealSale(@NonNull DealRequestBean bean) {
        mDealModel.deal(bean, new BaseObserver<AllOrderBean.DataBean>() {

            @Override
            public void onNext(@NonNull String msg, @NonNull AllOrderBean.DataBean dataBean) {
                mView.saleSuccess(dataBean);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.onDealFail();
                if (errType == IApi.CODE_MEANS_OF_EXCHANGE) {
                    mView.onResultError(errType, errMessage);
                } else {
                    mView.showToast(errMessage);
                }
            }
        }, mView.<BaseBean<AllOrderBean.DataBean>>bindAutoDispose());
    }
}
